<?php

namespace App\Http\Services;

use App\Models\Course;
use App\Models\CourseTestPaper;
use App\Models\CourseXZ;
use App\Models\ProColumn;
use Auth;
use Illuminate\Database\Eloquent\Model;

class CourseService
{
    public function Typeupdate($item)
    {
        $data = new ProColumn();
        if(isset($item['id']) && $item['id'] > 0){
            $data = ProColumn::find($item['id']);
            if(!$data){
                return false;
            }
        }else{
            $data->create_time = time();
        }
        $data->title = $item['title'];
        $data->icon_font = $item['icon_font'] ?? '';
        $data->parent_id = $item['parent_id'];
        $data->update_time = time();
        $data->save();
        if($data->id){
            return true;
        }
        return  false;
    }

    public function update($item)
    {
        $data = new CourseXZ();
        if(isset($item['id'])){
            $data = CourseXZ::find($item['id']);
            if(!$data){
                return false;
            }
        }else{
            $data->create_time = time();
        }

        $data->name = $item['name'] ?? '';
        $data->top_category = $item['top_category'] ?? 0;
        $data->secondary_category = $item['secondary_category'] ?? 0;
        $data->grade = $item['grade'] ?? 0;
        $data->is_off = $item['is_off'] ?? 0;
        $data->info = $item['info'] ?? '';
        $data->synopsis = $item['synopsis'] ?? '';
        $data->cover_img = $item['cover_img'] ?? '';
        $data->update_time = time();
        try {
            $data->save();
            if($data->id){
                return true;
            }
            return  false;
        } catch (\Throwable $th) {
            throw $th;
        }
        
    }

    public function storeQuestion($item)
    {
        $question = explode(',',$item['question']);
        $insert = [];
        foreach ($question as $k => $v) {
            $arr = [];
            $arr['question_id'] = $v;
            $arr['course_id'] = $item['course_id'];
            $arr['sid'] = 1;
            $arr['eid'] = 0;
            $arr['create_time'] = time();
            $arr['update_time'] = time();
            $insert[$k] = $arr;
        }
        try {
            CourseTestPaper::insert($insert);
            return true;
        } catch (\Throwable $th) {
            throw $th;
            return  false;
        }
    }
    
}
